//
//  NKCloudViewController.m
//  NewKY
//
//  Created by MXQ on 2016/11/14.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKCloudViewController.h"
#import "NKCloudChoseView.h"
@interface NKCloudViewController ()

@property(nonatomic,weak)NKCloudChoseView * cloudChoseView;

@end

@implementation NKCloudViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self confirmButton];
    
    [self setupView];
    
    
    
    NSLog(@"***%@",self.couldSaveList);
    
    
}

-(void)viewDidAppear:(BOOL)animated{


    //已经记忆的选择
    [self rememberCouldChiced];
    
}

//已经记忆的选择
-(void)rememberCouldChiced{

    [self.couldSaveList enumerateObjectsUsingBlock:^(NSString * obj1, NSUInteger idx1, BOOL * _Nonnull stop) {
       
        [self.cloudChoseView.cloudList enumerateObjectsUsingBlock:^(NSString * obj2, NSUInteger idx2, BOOL * _Nonnull stop) {
            
            
            if ([obj1 isEqualToString:obj2]) {
                
                NSLog(@"%zd",idx2);
                
                [self stepTwoWithNsinter:idx2];
                
            }
        }];
        
    }];
    



}

-(void)stepTwoWithNsinter:(NSUInteger )idex{

    UITableViewCell * cell = [self.cloudChoseView.tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:idex inSection:0] ];
    
    UIButton * btn = (UIButton *)cell.accessoryView;
    
    btn.selected = !btn.selected;
    
    //    for (NSString * item  in self.cloudesChosed) {
    //
    //        if ([self.cloudList[indexPath.row] isEqualToString:item] ) {
    //
    //            [self.cloudesChosed removeObject:item];
    //
    //        }
    //    }
    //
    
    [self.cloudChoseView.cloudesChosed enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([self.cloudChoseView.cloudList[idex] isEqualToString:obj] ) {
            
            *stop = YES;
            
            if (*stop == YES) {
                
                [self.cloudChoseView.cloudesChosed removeObject:obj];
                
            }
        }
    }];
    
    
    if (btn.selected) {
        
        [self.cloudChoseView.cloudesChosed addObject:self.cloudChoseView.cloudList[idex]];
        
    }
    
    NSLog(@"%@",self.cloudChoseView.cloudesChosed);



}




//添加确定按钮
- (void)confirmButton{

    
    
    
    UIBarButtonItem * confirmButton = [[UIBarButtonItem alloc]initWithTitle:@"确定"
                                                                      style: UIBarButtonItemStyleDone target:self action:@selector(confirmAction)];
    
    self.navigationItem.rightBarButtonItem = confirmButton;



}


//确定点击返回数组到主界面
-(void)confirmAction{
    //回调
    self.illCloudDidSelectBlock(self.cloudChoseView.cloudesChosed.copy);
    
    
    //pop出去
    [self.navigationController popViewControllerAnimated:YES];
}



//建设tableView
- (void)setupView{

    NKCloudChoseView * choseView = [[NKCloudChoseView alloc]initWithFrame:self.view.bounds];
    
    [self.view addSubview:choseView];
    
    self.cloudChoseView = choseView;

}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
